/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.configuration.stLuzia;

import org.ipti.br.tag.bean.SessionBean;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.configuration.qualifiers.Responsible;
import org.ipti.br.tag.entity.Unity;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

/**
 *
 * @author luancorumba
 */
@ApplicationScoped
public class UnityFactory {
    
    @Inject
    private UnityBO unityBO;
    
    @Produces
    @Responsible
    public Unity findBoss(SessionBean sessionBean) {
        Unity unity = sessionBean.getLoggedActor().getUnity();
        return getUnity(unity);
    }
    
    private Unity getUnity(Unity unity) {
        List<Unity> list = unityBO.getUnityChildren(unity.getId());
        
        for (int i = 0; i < list.size(); i++) {
            if (!list.get(i).getOrganization().getName().equals("Setor Pedagógico")) {
                list.addAll(unityBO.getUnityChildren(list.get(i).getId()));
            } else {
                return list.get(i);
            }
        }
        
        return null;
    }
}